<?php

namespace Xrfg\Api\Trading\Account\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Xrfg\Admin\Common\Models\AccountWorkflows;
use Xrfg\Api\Trading\Account\Requests\AccountCapitalflowRequest;
use Xrfg\Api\Trading\Account\Resources\AccountCapitalflowResource;
use Xrfg\Core\Trading\Account\Models\AccountCapitalflow;
use Xrfg\Core\Trading\Account\Models\Recharge;
use Xrfg\Core\Trading\Account\Models\Transfer;
use Xrfg\Core\Trading\Account\Models\Withdraw;

class AccountCapitalflowController extends Controller
{
    /**
     * 总账户资金流水.
     *
     * @param AccountCapitalflowRequest $request
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(AccountCapitalflowRequest $request)
    {
        $coinId = $request->query('coin_id');
        $type = $request->query('type');  // 1：充币 2：提币 3：由以太星球转入
        $start = $request->query('start_time');
        $end = $request->query('end_time');
        $time = $request->query('time'); // 1-最近7天 2-7天至1个月 3-7天至3个月
        $perPage = $request->query('per_page', 15);
        $perPage = $perPage > 100 ? 100 : $perPage;

        $accountCapitalflows = AccountCapitalflow::where('user_id', Auth::id())
            ->when(null !== $coinId, function (Builder $query) use ($coinId) {
                return $query->where('coin_id', $coinId);
            })
            ->when(in_array($type, [1, 2, 3, 4, 5]), function (Builder $query) use ($type) {
                if (1 == $type) { // 充币
                    return $query->where('class_id', Recharge::CLASS_ID);
                } elseif (2 == $type) { // 提币
                    return $query->where('class_id', Withdraw::CLASS_ID)
                        ->where('op_id', Withdraw::OP_APPLY);
                } elseif (3 == $type) { // 由合约账户转入
                    return $query->where('class_id', Transfer::CLASS_ID)
                        ->where('op_id', Transfer::OP_ACCOUNT_IN);
                } elseif (4 == $type) { // 转出至合约账户
                    return $query->where('class_id', Transfer::CLASS_ID)
                        ->where('op_id', Transfer::OP_ACCOUNT_OUT);
                } elseif (5 == $type) { // 系统
                    return $query->where('class_id', AccountWorkflows::CLASS_ID);
                }
            })
            ->when(!in_array($type, [1, 2, 3, 4, 5]), function (Builder $query) {
                return $query->whereIn('class_id', [1, 2, 3]);
            })
            ->when(null !== $start, function (Builder $query) use ($start) {
                return $query->where('created_at', '>=', $start);
            })
            ->when(null !== $end, function (Builder $query) use ($end) {
                return $query->where('created_at', '<=', $end);
            })
            ->when($time == 1, function (Builder $query) { // 1-最近7天
                return $query->where('created_at', '>=', now()->subDays(7));
            })
            ->when($time == 2, function (Builder $query) { // 2-7天至1个月
                return $query->where('created_at', '>=', now()->subMonth())->where('created_at', '<', now()->subDays(7));
            })
            ->when($time == 3, function (Builder $query) { // 2-7天至3个月
                return $query->where('created_at', '>=', now()->subMonths(3))->where('created_at', '<', now()->subDays(7));
            })
            ->orderByDesc('id')
            ->paginate($perPage)
            ->appends($request->only(['coin_id', 'type', 'start_time', 'end_time', 'time', 'per_page']));

        return AccountCapitalflowResource::collection($accountCapitalflows);
    }
}
